﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace Cookbook.Common
{
    public static class PropertyChangedEventArgsExtensions
    {
        public static bool MatchesProperty(this PropertyChangedEventArgs arg, Expression<Func<object>> expression )
        {
            return (arg.PropertyName == GetMemberName(expression.Body));
        }

        private static string GetMemberName(Expression expression)
        {
            if (expression is MemberExpression)
            {
                var memberExpression = (MemberExpression)expression;

                return memberExpression.Member.Name;
            }

            if (expression is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)expression;

                return GetMemberName(unaryExpression.Operand);
            }

            return string.Empty;
        }
    }
}